const Mock = require('mockjs')
const { deepClone } = require('./utils')
import asyncRoutes from './role/routes'
const routes = deepClone([...asyncRoutes])
const tokens = {
  '18981818900': {
    token: 'admin-token',
    refresh_token: 'refresh-token',
    uuid: 'asdfasdfasf45454',
    mfa: 0,
    fce: 0,
    auths: routes,
    expire: 7200,
    user_info: {
      id: 0,
      role: 1,
      role_name: '超级管理员',
      introduction: '我是超级管理员',
      account: '18981818900',
      avatar: '',
      name: '锐狐超管'
    }
  },
  editor: {
    token: 'editor-token'
  }
}

const users = [
  {
    id: 0,
    role: '超级管理员',                                                                   //角色
    introduction: '我是超级管理员',
    account: '18981818900',                                                             //账号
    avatar: '',                                                                         //头像
    name: '锐狐超管',                                                                    //名字
    recent_login_time: '2022-10-10 10:28:29',                                          //最近登录时间
    recent_login_ip: '192.168.1.1 四川成都',                                             //ip
    login_device: '登录设备12',
    strategy: [                                                                        // 绑定策略
      {value: 1,label: '栏目管理员'},
      {value: 2,label: '阿道夫管理员'},
      {value: 3,label: '妇产科管理员'},
      {value: 4,label: '项目管理员'},
    ]
  },
  {
    id: 1,
    role: 2,
    introduction: '我是一个普通管理员',
    account: '18990340787',
    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
    name: '普通管理员',
    strategy: []
  }
]

export default [
  // 用户登录
  {
    url: '/auth/auth/login',
    type: 'post',
    data: config => {
      const { account } = JSON.parse(config.body)
      const token = tokens[account]
      if(process.env.NODE_ENV === 'development') console.log(token)
      // mock error
      if (!token) {
        return {
          code: 60204,
          message: '账号或者密码错误！'
        }
      }
      if(Math.random()*3<1) {
        return {
          code: 20000,
          data: {
            token: 'admin-token',
            mfa: 0,
            fce: 0,
            auths: routes,
            user_info: {
              id: 0,
              role: 1,
              introduction: '我是超级管理员',
              account: '18981818900',
              avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
              name: '锐狐超管'
            }
          }
        }
      }
      return {
        code: 20000,
        data: {
          token: 'admin-token',
          refresh_token: 'refresh-token',
          mfa: 1,
          fce: 0,
          user_info: {
            account: '18981818900',
          }
        }
      }
    }
  },
  //身份验证
  {
    url: '/auth/auth/idAuth',
    type: 'post',
    data: config => {
      JSON.parse(config.body)
      let { auth_code } = JSON.parse(config.body)
      const token = tokens['18981818900']
      if(process.env.NODE_ENV === 'development') console.log(token)
      if (auth_code !== '5438') {
        return {
          code: 60204,
          message: '验证码错误！'
        }
      }
      return {
        code: 20000,
        message: 'success',
        data: token
      }
    }
  },
  // 获取验证码
  {
    url: '/auth/auth/getCode',
    type: 'post',
    data: () => {
      return {
        code: 20000,
        message: 'success'
      }
    }
  },
  // 获取短信验证码
  {
    url: '/auth/auth/getMsgCode',
    type: 'post',
    data: () => {
      return {
        code: 20000,
        data: {
          auth_token: 'admin-token',
          status_color: 'error'
        },
        message: 'success'
      }
    }
  },
  // 修改密码
  {
    url: '/auth/auth/updatePassword',
    type: 'post',
    data: () => {
      return {
        code: 20000,
        message: 'success'
      }
    }
  },
  // 重置密码
  {
    url: '/auth/auth/resetPassword',
    type: 'post',
    data: () => {
      return {
        code: 20000,
        message: 'success'
      }
    }
  },
  // 账号检测
  {
    url: '/auth/auth/accountDetection',
    type: 'get',
    data: (config) => {
      if(process.env.NODE_ENV === 'development') console.log(config)
      return {
        code: 20000,
        data: Mock.mock({
          'is_exist|1': [0,1],
          'token':'admin-token'
        })
      }
    }
  },
  // 手机验证码验证
  {
    url: '/auth/auth/validateCode',
    type: 'post',
    data: (config) => {
      let { var_code } = JSON.parse(config.body)
      if (var_code !== '5438') {
        return {
          code: 60204,
          message: '验证码错误！'
        }
      }
      return {
        code: 20000,
        message: 'success'
      }
    }
  },
  // 获取用户信息
  {
    url: '/auth/auth/userInfo',
    type: 'get',
    data: () => {
      const info = users[0]
      return {
        code: 20000,
        data: info
      }
    }
  },
  //修改头像
  {
    url: '/auth/auth/editAvatar',
    type: 'post',
    data: () => {
      return {
        code: 20000,
        message: 'success'
      }
    }
  },
  // 修改账号
  {
    url: '/auth/auth/editAccount',
    type: 'post',
    data: () => {
      return Mock.mock({
        code: 20000,
        message: 'success'
      })
    }
  },
  // 修改密码
  {
    url: '/auth/auth/userEditPassword',
    type: 'post',
    data: () => {
      return Mock.mock({
        code: 20000,
        message: 'success'
      })
    }
  },
  // 退出登录
  {
    url: '/auth/auth/logout',
    type: 'post',
    data: () => {
      return {
        code: 20000,
        data: 'success'
      }
    }
  },
  // 刷新token
  {
    url: '/auth/auth/refreshToken',
    type: 'post',
    data: () => {
      return {
        code: 20000,
        data: {
          expire: 7200,
          token: 'admin-token',
          refresh_token: 'refresh-token',
          uuid: 'asdf44s5ad4f555s4d'
        }
      }
    }
  },
  // 获取登录信息列表
  {
    url: '/auth/auth/getLoginMessage',
    type: 'get',
    data: () => {
      return {
        code: 20000,
        data: Mock.mock({
          'list|5':[
            {
              'id|+1': 1,
              'status|1': [0,1],
              'device|1': ['pc_12138','iphone_4555','pc_5555'],
              'ip|1': ['192.168.1.1 四川成都','192.168.1.1 美国'],
              'ip_status|1': [0,1],
              'created_at': '@datetime'
            }
          ],
          total: 100,
          pageSize: 5,
          currentPage: 1
        })
      }
    }
  },
  // 获取所有用户信息
  {
    url: '/provider/user/userList',
    type: 'get',
    data: () => {
      return {
        code: 20000,
        data: Mock.mock({
          'list|15':[
            {
              'id|+1': 1,
              'role|1': [1,2],
              introduction: '@ctitle(6,11)',
              account: '@string("number", 11, 11)',
              avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
              name: '@cname',
              'strategy|1': [[1,2,3],[]],
            }
          ],
          total: 100,
          pageSize: 15,
          currentPage: 1
        })
      }
    }
  },
  // 添加用户
  {
    url: '/auth/user/addUser',
    type: 'post',
    data: () => {
      return {
        code: 20000,
        message: 'success'
      }
    }
  },
  // 删除用户
  {
    url: '/auth/user/deleteUser',
    type: 'post',
    data: () => {
      return {
        code: 20000,
        message: 'success'
      }
    }
  },
  // 编辑用户
  {
    url: '/auth/user/editUser',
    type: 'post',
    data: () => {
      return {
        code: 20000,
        message: 'success'
      }
    }
  },
  // 获取用户菜单及路由列表
  {
    url: '/auth/menu/getRoutes',
    type: 'get',
    data: () => {
      return {
        code: 20000,
        data: routes
      }
    }
  },
  // 修改菜单isshow
  {
    url: '/auth/user/menuIsShow/change',
    type: 'post',
    data: () => {
      return {
        code: 20000,
        message: 'success'
      }
    }
  },
  // 用户绑定策略
  /*
  * {strategy: []}
  * */
  {
    url: '/auth/user/bindStrategy',
    type: 'post',
    data: () => {
      return {
        code: 20000,
        message: 'success'
      }
    }
  },
]
